/**
 * 功能 把数据转成适合listSelection组件格的（只针对特定数据格式--私有工具）
 *var data={"id":"4","parentId":"-1","children":[{"id":"1970","parentId":"4","children":[],"name":"永升物业太仓分公司","orgLevel":2,"projects":null,"order":3}],"name":"上海永升物业管理股份有限公司","orgLevel":0,"projects":null,"order":1}
 *
 *func(data) 即可返回合法的
 * @Author: lizuping
 * @Date: 2018-00-00
 * @Last Modified by: lizuping
 * @Last Modified time: 2018-07-23 19:45:28
 */

export default function (data) {
    var put = { title: '无', listData: [] };
    if (parseInt(data.orgLevel) === 0) {
        put.title = data.name;
    }
    var listData = getOrg(data.children);
    var len = listData.length;
    for (var i = 0;i < len;i++) {
        var tempObj = {name: '', show: false, child: [], obj: listData[i]};
        var arrl = listData[i].length;
        for (var n = 0;n < arrl;n++) {
            tempObj.name += listData[i][n].name + '/';
        }
        tempObj.name = tempObj.name.slice(0, -1);
        tempObj.child = listData[i][arrl - 1].children;
        put.listData.push(tempObj);
    }
    return put;
    function getOrg (list) {
        var allTemp = [];
        var n = list.length;
        for (var i = 0;i < n;i++) {
            var temp = [];
            hasOrg(list[i], temp);
            if (temp.length > 0 && temp[temp.length - 1].orgLevel === 4) {
                temp.pop();
                allTemp.push(temp);
            }
        }
        return allTemp;
    }
    function hasOrg (d, p) {
        if (d.children && d.children.length > 0) {
            if (d.orgLevel < 4) {
                p.push(d);
                hasOrg(d.children[0], p);
            } else if (d.orgLevel === 4) {
                p.push(d);
            }
        } else if (d.orgLevel === 4) {
            p.push(d);
        }
    }
}
